/**
 * Unix时间戳或者毫秒时间戳转换成时间
 * Created by zhuyongjie on 2017/10/19.
 */
/**
 *
 * @param value毫秒或Unix时间戳
 * @param type 1：代表Unix时间戳；2：毫秒时间戳
 * @returns {string}
 */
function timedate(value,type) {
    var source = value;
    var str_tmp = '', result = '', t = '';
    var str_split = source == null ? '' : source.toString().split("\n");

    if(type > 2) {
        var zone = new Date().getTimezoneOffset() / 60;
        for(var i in str_split) {
            // 跳过unique
            if(typeof str_split[i] == 'function') continue;
            if(str_split[i].length < 4) {
                str_tmp += str_split[i] + "\n";
                continue;
            }
            t = str_split[i].match(/([\d]{0,4})[/年-]([\d]{0,2})[/月-]([\d]{0,2})[/日-]?[\s]?([\d]{0,3})?[时:]?([\d]{0,2})?[分:]?([\d]{0,2})?[秒:]?([\d+]{0,3})?/);
            // 未匹配到正确结果
            if(t) {
                result = new Date(Date.UTC(parseInt(t[1]), parseInt(t[2] - 1), parseInt(t[3]), parseInt(t[4]||0) + zone, parseInt(t[5]||0), parseInt(t[6]||0), parseInt(t[7]||0)));
                // 弹窗选项
                str_tmp += (t[7] || $('#myModal-timedate input:checked').val() == 2)  ? result.getTime() : result.getTime() / 1000;
            } else {
                str_tmp += str_split[i];
            }
            str_tmp += "\n";
        }

        // 默认值
        if(!$.trim(str_tmp)) {
            str_tmp = new Date().getTime();
            str_tmp = Math.floor(str_tmp / 1000) + "\n" + str_tmp;
        }
    } else {

        for(var i in str_split) {
            // 跳过unique
            if(typeof str_split[i] == 'function') continue;
            // 判断是否值合法
            if(str_split[i].length > 1 && $.trim(str_split[i]) && !isNaN(str_split[i])) {
                // 10位时间戳，用毫秒时间戳功能转换时，自动补全000
                t = str_split[i] ? new Date(parseInt(str_split[i]) * (type == 1 ? 1000 : (str_split[i].length == 10 ? 1000 : 1))) : new Date();
                str_tmp += t.getFullYear() + "-"
                    + ((t.getMonth() + 1) < 10 ? ('0' + (t.getMonth() + 1)) : (t.getMonth() + 1))
                    + "-" + (t.getDate() < 10 ? ('0' + t.getDate()) : t.getDate())
                    + " " + (t.getHours() < 10 ? ('0' + t.getHours()) : t.getHours())
                    + ":" + (t.getMinutes() < 10 ? ('0' + t.getMinutes()) : t.getMinutes() )
                    + ":" + (t.getSeconds() < 10 ? ('0' + t.getSeconds()) : t.getSeconds());
                type == 2 && (str_tmp += "." + t.getMilliseconds() + "");
            } else {
                // 归还原值
                str_tmp += str_split[i];
            }
            str_tmp += "\n";
        }
    }
    return str_tmp;
};

/**
 * 格式化 format日期方法   yyyy-MM-dd
 * @param format
 * @returns {*}
 */
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "h+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
            RegExp.$1.length == 1 ? o[k] :
                ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

/**
 * 计算天数差的函数，通用
 */
function  DateDiff(sDate1,  sDate2){    //sDate1和sDate2是2017-08-01格式
    var sDate1year = sDate1.split("-")[0];
    var sDate1month = sDate1.split("-")[1]-1;
    var sDate1day = sDate1.split("-")[2];
    var applydate1 = new Date(sDate1year,sDate1month,sDate1day );

    var applyyear = sDate2.split("-")[0];
    var applymonth = sDate2.split("-")[1]-1;
    var applyday = sDate2.split("-")[2];
    var applydate2 = new Date(applyyear,applymonth,applyday );
    var iDays =parseInt((applydate1-applydate2) / (1000 * 60 * 60 * 24));
    return  iDays;
}